package org.wmine.algo;
import java.io.*;
import java.math.*;

class BasicAlgo_Anu{
	double mean=0,median=0,mode=0;	//contains array on which operations to perform
	 double []array;
	 double []array1;
	public BasicAlgo_Anu(double[]a1,double[]a2)
	{
	//array[0]=3.00;
	//array1[0]=3.1;
		array=new double[10];
		array1=new double[10];
		array=a1;
		array1=a2;
BasAlgoWriter baw=new BasAlgoWriter("stat-result.xml","stat-result");
		
		String element[][]=new String[2][5];
		String values[][]=new String[2][5];
		
		element[0][0]="name";
		values[0][0]="x";
		element[0][1]="mean";
		values[0][1]=String.valueOf(calculateMean(array));	
		element[0][2]="median";
		values[0][2]=String.valueOf(calculateMedian(array));	
		element[0][3]="mode";
		values[0][3]=String.valueOf(calculateMode(array));	
		element[0][4]="standard-dev";
		values[0][4]=String.valueOf(calculateStdDev(array));
	//	baw.addElement("result",element,values);
		
		element[1][0]="name";
		values[1][0]="y";
		element[1][1]="mean";
		values[1][1]=String.valueOf(calculateMean(array1));	
		element[1][2]="median";
		values[1][2]=String.valueOf(calculateMedian(array1));	
		element[1][3]="mode";
		values[1][3]=String.valueOf(calculateMode(array1));	
		element[1][4]="standard-dev";
		values[1][4]=String.valueOf(calculateStdDev(array1));
		baw.addElement("basic","result",element,values);
			
		System.out.println(baw.getXMLString());
		String element1[][]=new String[2][3];
		String values1[][]=new String[2][3];
		
		element1[0][0]="attribute1";
		values1[0][0]="x";
		element1[0][1]="attribute2";
		values1[0][1]="y";
		element1[0][2]="value";
		values1[0][2]=String.valueOf(calculateCorrCoeff(array,array1));
		
		element1[1][0]="attribute1";
		values1[1][0]="a";
		element1[1][1]="attribute2";
		values1[1][1]="b";
		element1[1][2]="value";
		values1[1][2]=String.valueOf(calculateCorrCoeff(array,array1));
		
		baw.addElement("correlation","result",element1,values1);
		
		String element2[][]=new String[2][4];
		String values2[][]=new String[2][4];
			double x[]=calculateLinReg(array,array1);
			System.out.println(x[0]+","+x[1]);
		
		element2[0][0]="attribute1";
		values2[0][0]="x";
		element2[0][1]="attribute2";
		values2[0][1]="y";
		element2[0][2]="slope";
		values2[0][2]=String.valueOf(x[1]);
		element2[0][3]="intercept";
		values2[0][3]=String.valueOf(x[0]);
		
		element2[1][0]="attribute1";
		values2[1][0]="a";
		element2[1][1]="attribute2";
		values2[1][1]="b";
		element2[1][2]="slope";
		values2[1][2]=String.valueOf(x[1]);
		element2[1][3]="intercept";
		values2[1][3]=String.valueOf(x[0]);
		
		
		baw.addElement("Linear-Regression","result",element2,values2);
		baw.save();	
		System.out.println(baw.getXMLString());
		
		//frequency dist has a separate xml doc
			int interval=4;
			BasAlgoWriter baw1=new BasAlgoWriter("Freq-Dist.xml","fd");
			//double[][] freqDist=ba.freqDist(array,interval);
			double[][] freqDist=freqDist(array,interval);
			String element3[][]=new String[interval][3];
			String values3[][]=new String[interval][3];
			baw1.addElement("no_of_intervals",String.valueOf(interval));
			baw1.addElement("attribute-name","x");
			for(int i=0;i<freqDist[0].length;i++)
			{	element3[i][0]="lower";
				values3[i][0]=String.valueOf(freqDist[0][i]);
				element3[i][1]="upper";	
				values3[i][1]=String.valueOf(freqDist[1][i]);
				element3[i][2]="freq";
				values3[i][2]=String.valueOf(freqDist[2][i]);	
			
			}
			baw1.addElement("class",element3,values3);
			baw1.save();
			System.out.println(baw1.getXMLString());
			

	}

		
	double calculateMean(double []num_array){
		 //mean is nothing but average
		 mean=0;
		for(int i=0;i<num_array.length;i++)
			mean+=num_array[i];
		mean/=num_array.length;
		return mean;
	}
	
	double calculateMedian(double []num_array){
		 //median is element at middle position
		int position=0,size=0;
		median=0;
		size=num_array.length;
		if(size % 2 != 0)
			position = (size + 1)/2;
		else
			position = ((size/2)+( (size+1) /2))/2;
		double sorted_arr[]=sort(num_array);
	
		median=sorted_arr[position-1];
		return median;
	}
	
	double[] sort(double []num)
	{	double arr[]=num,temp;
		for(int i=0;i<arr.length;i++)
		{	for(int j=0;j<i;j++)
			{	if(arr[i] < arr[j])
				{	temp=arr[i]	;
					arr[i]=arr[j];
					arr[j]=temp;
				}
			}
		}
		for(int i=0;i<arr.length;i++)
			System.out.print(arr[i]);
			System.out.println();
		return arr;
	}
	
	double calculateMode(double []num_array){
		double max_occured=0;
		mode=0;
		double count=0;//mode is element occuring maximum times
		for(int i=0;i<num_array.length;i++)
		{	int temp_count=0;
			double temp_max=num_array[i];
			for(int j=0;j<num_array.length;j++)
			{		if(temp_max == num_array[j])
						temp_count++;
			
			}
			if(temp_count > count)
			{	count=temp_count;
				max_occured=num_array[i];
			}
		}
		mode = max_occured;
		return mode;
	}
	
	double[][] freqDist(double []num_array,int no_of_intervals){
		double min=0,max=0,luf[][];
		double class_interval=0;
		//int frequency[];
		//lower_limits=new double[no_of_intervals];
		//upper_limits=new double[no_of_intervals];
		//frequency=new int[no_of_intervals];
		luf=new double[3][no_of_intervals];
		System.out.println("length"+num_array.length);
		min=max=num_array[0];
		for(int i=0;i<num_array.length;i++){
			if(min>num_array[i])
				min=num_array[i];
			if(max<num_array[i])
				max=num_array[i];
			
			}
		System.out.println(min);
		System.out.println(max);
		
		class_interval=(max-min)/no_of_intervals;
	
		System.out.println(class_interval);
		
	
		
		for(int i=0;i<no_of_intervals;i++){
			if(i==0)
				luf[0][i]=min;
			else
				luf[0][i]=luf[1][i-1];
				
			luf[1][i] = luf[0][i]+class_interval;
					
			System.out.println(luf[0][i]);
			System.out.println(luf[1][i]);
		}
		
		for(int i=0;i<num_array.length;i++){
			for(int j=0;j<no_of_intervals;j++)
			{	if(((num_array[i]>=luf[0][j]) && (num_array[i]<luf[1][j]))) 
				{	luf[2][j]++;
					break;
				}
				else if(num_array[i] == max)
				{	luf[2][no_of_intervals-1]++;
					break;
				}
			}
		}
		for(int j=0;j<no_of_intervals;j++)
			System.out.println(luf[2][j]);
		return luf;	
	}
	
	
	
	double calculateStdDev(double []num_array){
		double sum=0.0;
		int size=num_array.length;
		if(mean == 0) 
			calculateMean(num_array);
		for(int i=0;i<size;i++)
			sum = sum + ((num_array[i] - mean) * (num_array[i] - mean));
		
		return Math.sqrt(sum/(size-1));
	}
	
	double calculateCorrCoeff(double []num_array1,double []num_array2){
		double corr_coeff=0,mean1=0,mean2=0;
		mean1=calculateMean(num_array1);
		mean2=calculateMean(num_array2);
		if(num_array1.length == num_array2.length)
			for(int i=0;i<num_array1.length;i++)
				corr_coeff = corr_coeff+((num_array1[i]-mean1)*(num_array2[i]-mean2));
				
				
		corr_coeff=corr_coeff/(num_array1.length*(calculateStdDev(num_array1))*(calculateStdDev(num_array2)));
		return corr_coeff;
	}
	
	double[] calculateLinReg(double []num_array1,double []num_array2){
		double num=0,denom=0,mean1=0,mean2=0,w[];
		w=new double[2];
		mean1=calculateMean(num_array1);
		mean2=calculateMean(num_array2);
		
		if(num_array1.length == num_array2.length)
		{	for(int i=0;i<num_array1.length;i++)
			{	num = num+((num_array1[i]-mean1)*(num_array2[i]-mean2));	
				denom=denom+((num_array1[i]-mean1)*(num_array1[i]-mean1));
			}
		}
		w[1]=num/denom;
		w[0]=mean2-mean1*w[1];
		return w;
		
	}
	public static void main(String args[]){
	double ar1[]={1,2,3,4,5,6,7,8,9,0};
	double ar2[]={1,2,3,4,5,6,7,8,9,0};
		BasicAlgo_Anu ba =	new BasicAlgo_Anu(ar1,ar2);
			}
}